function Account() {
}

Account.prototype = {
    moodmillExtensionURI: 'chrome://moodmill',
    currentURI: 'http://www.moodmill.com',
    getAuthInfo: function(username, idx) {
        var passwordManager = this.getPasswordManager();
        // the host name of the password we are looking for
        var queryString = this.currentURI;
        // ask the password manager for an enumerator:
        var e = passwordManager.enumerator;
        
        var recIdx = 0;
        if (idx>0)
            recIdx = idx;
    
        // step through each password in the password manager until we find the one we want:
        while (e.hasMoreElements()) {
            try {
                // get an nsIPassword object out of the password manager.
                // This contains the actual password...
                pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
                if ((pass.host == queryString) && (pass.user == username)) {
                    if (recIdx > 0) {
                        recIdx--;
                    } else {
                        // found it!]
                        return pass;
                        break;
                    }
                }
            } catch (ex) {
                // do something if decrypting the password failed--probably a continue
                return null;
            }
        }
        return null;
    },
    getAllAuthInfo: function() {
    Logger.log("getAllAuthInfo: " + this.currentURI);
        var passwordManager = this.getPasswordManager();
        // the host name of the password we are looking for
        var queryString = this.currentURI;
        // ask the password manager for an enumerator:
        var e = passwordManager.enumerator;
        var res = null;
        // step through each password in the password manager until we find the one we want:
        while (e.hasMoreElements()) {
            try {
                // get an nsIPassword object out of the password manager.
                // This contains the actual password...
                pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
                if (pass.host == queryString) {
                    if (res==null)
                        res = new Array();
                    res.push(pass);
                    Logger.log("getAllAuthInfo: found " + pass.user);
                }
            } catch (ex) {
                // do something if decrypting the password failed--probably a continue
                return null;
            }
        }
        return res;
    },
    setAccountURI: function(URI) {
        this.currentURI = URI;
    },
    update: function(username, password) {
        this.setAccountURI(this.moodmillExtensionURI);
        if (this.getAuthInfo(username, 0))
            this.removeAccount(username);
        this.addAccount(username, password);
    },
    addAccount: function(username, password) {
        var passwordManager = this.getPasswordManager();
        passwordManager.addUser(this.currentURI, username , password);
    },
    getPasswordManager: function() {
        var passwordManager = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);
        
        return passwordManager;
    }, 
    removeAccount: function(username) {
        var passwordManager = this.getPasswordManager();
        passwordManager.removeUser(this.currentURI, username);
    }
}

if (typeof YAHOO.MoodMill.Account == "undefined") 
    YAHOO.MoodMill.Account = new Account();